﻿namespace Charades.Web.Helpers
{
	using System;
	using System.Collections.Specialized;
	using System.Net;
	using System.Threading.Tasks;

	public static class YoHelper
	{
		private static readonly Uri YoUrl = new Uri("http://api.justyo.co/yo/");

		public static void SendYo()
		{
			if (!ServerSettings.IsYoAdminsNotifying || string.IsNullOrEmpty(ServerSettings.YoApiToken))
			{
				return;
			}

			Task.Run(() => SenYoInternal());
		}

		private static void SenYoInternal()
		{
			using (var wr = new WebClient())
			{
				var data = new NameValueCollection();
				data["api_token"] = ServerSettings.YoApiToken;
				foreach (var yoUser in ServerSettings.YoUsers)
				{
					data["username"] = yoUser;
					wr.UploadValues(YoUrl, data);
				}
			}
		}
	}
}